# Maintainer: Christoph Reiter <reiter.christoph@gmail.com>

_realname=podman
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=4.6.0
pkgrel=1
pkgdesc='Tool for running OCI-based containers in pods (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/containers/podman"
license=('spdx:Apache-2.0')
options=('!strip')
makedepends=(
    "${MINGW_PACKAGE_PREFIX}-go"
    "${MINGW_PACKAGE_PREFIX}-cc"
    "${MINGW_PACKAGE_PREFIX}-python"
    "git"
)
source=("${_realname}-${pkgver}.tar.gz::https://github.com/containers/podman/archive/refs/tags/v$pkgver.tar.gz")
sha256sums=('a1b4fd7c3a9b850ab4b405f8d03115248a780c89b11e7d8ffbba2d1f668f678e')
noextract=("${_realname}-${pkgver}.tar.gz")

prepare() {
    echo "Extracting ${_realname}-${pkgver}.tar.gz ..."
    tar -xzf ${_realname}-${pkgver}.tar.gz | true
    rm -rf build-${MSYSTEM}
    cp -r ${_realname}-${pkgver} build-${MSYSTEM}
}

build() {
    cd "build-${MSYSTEM}"

    export GOOS=windows
    export GOROOT=${MINGW_PREFIX}/lib/go
    export CGO_CPPFLAGS="${CPPFLAGS}"
    export CGO_CFLAGS="${CFLAGS}"
    export CGO_CXXFLAGS="${CXXFLAGS}"
    export CGO_LDFLAGS="${LDFLAGS}"
    export GO_LDFLAGS="-s -w"
    export GOFLAGS="-trimpath -modcacherw -ldflags=-linkmode=external"
    case "${CARCH}" in
      i686|x86_64)
        GOFLAGS+=" -buildmode=pie"
        ;;
    esac

    # parallel make breaks markdown processing
    make -j1 podman-remote
    make -j1 docker-docs
    make -j1 win-gvproxy
}

package() {
    cd "build-${MSYSTEM}"

    make install.remote install.docker-full install.man install.completions DESTDIR="$pkgdir" PREFIX=${MINGW_PREFIX}
    rm -Rf "${pkgdir}${MINGW_PREFIX}/lib"
    sed -i "s|/usr/bin/||g" "${pkgdir}${MINGW_PREFIX}/bin/docker"

    cp bin/windows/win-sshproxy.exe "${pkgdir}${MINGW_PREFIX}/bin"
}
